'use strict'

import { app, protocol, BrowserWindow, ipcMain, clipboard,dialog } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
import path from 'path'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 450,
    minWidth: 800,
    minHeight: 450,
    frame: false,
    resizable:false,
    // icon:path.join(__dirname, '../SD/app-x.ico'),
    webPreferences: {
      webSecurity: true,
      nodeIntegration: true, //开启true这一步很重要,目的是为了vue文件中可以引入node和electron相关的API
      contextIsolation: false, // 可以使用require方法
    }
  })

  // 最小化
  ipcMain.on('window-minimize', function () {
    win.minimize()
  })

    // 退出程序,这里接收
    ipcMain.on('window-close', function () {
    // if (process.platform !== 'darwin') {
      win.destroy()
    // }
  })


    //关闭新建窗口
    ipcMain.on('window-closeF', (event,res) =>{
      if (res == null) {
        winn.destroy()
          winn = null
      }else{
        win.webContents.send('delList', res)
        setTimeout(() => {
          winn.destroy()
          winn = null
        }, 1000);
        }
    })


  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    // if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

app.on('ready', async () => {

  createWindow()

  //前端获取路径
  ipcMain.on('getPath', (event) =>{
    // event.reply('AppPath',app.getAppPath())  //开发环境路径
    event.reply('AppPath',path.dirname(app.getAppPath()))
  })
  //将前端传入的写入剪贴板
  ipcMain.on('copy', (event,res) =>{
    clipboard.writeText(res)
  })


  //打开
  ipcMain.on('window-openF', (event) =>{
    if (winn == null) {
      cartNew()
    }else{
      winn.focus()
    }
  })
  //直播提醒
  ipcMain.on('window-openT', (event,res) =>{
    cartNewTip()
    setTimeout(() => {
      openZB.webContents.send('data', res)
    }, 500);
    setTimeout(() => {
      openZB.destroy()
      openZB = null
    }, 4000);

  })
  //打开文件
  ipcMain.on('openFliePathS', (event) =>{

    dialog.showOpenDialog({
    title:'选择文件',
    properties:['openfile'],
    filters:[
      { name: 'Images', extensions: ['jpg', 'png', 'gif'] },
    { name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
    ]
  }).then(res =>{
    event.reply('openFliePathE',res)
  }).catch(err =>{
    
    })
  })
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

let winn = null
async function cartNew() {
  winn = new BrowserWindow({
    width: 600,
    height: 350,
    minWidth: 600,
    minHeight: 350,
    frame: false,
    resizable:false,
    modal:true,
    // icon:path.join(__dirname, '../SD/app-x.ico'),
    webPreferences: {
      webSecurity: true,
      nodeIntegration: true, //开启true这一步很重要,目的是为了vue文件中可以引入node和electron相关的API
      contextIsolation: false, // 可以使用require方法
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await winn.loadURL(process.env.WEBPACK_DEV_SERVER_URL+'/#/ddF')
    // if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    winn.loadURL('app://${__dirname}/index.html'+'#/ddF')
  }
}

let openZB = null
async function cartNewTip() {
  openZB = new BrowserWindow({
    x:20,
    y:20,
    width: 200,
    height: 100,
    frame: false,
    resizable:false,
    modal:true,
    alwaysOnTop:true,
    // icon:path.join(__dirname, '../SD/app-x.ico'),
    webPreferences: {
      webSecurity: true,
      nodeIntegration: true, //开启true这一步很重要,目的是为了vue文件中可以引入node和electron相关的API
      contextIsolation: false, // 可以使用require方法
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await openZB.loadURL(process.env.WEBPACK_DEV_SERVER_URL+'/#/ddT')
    // if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    openZB.loadURL('app://${__dirname}/index.html'+'#/ddT')
  }
}